package com.cloudptt.wp.ui.call.group;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudptt.api.product.func.call.CallConst;
import com.cloudptt.api.product.func.call.group.fsm.video.GroupCallMemberInfo;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.wp.R;

import java.util.List;

public class GroupCallMemberListAdapter extends BaseAdapter {

    private Context ctx = null;
    private boolean isView = false;
    private int selectedPos = 0;
    private List<GroupCallMemberInfo> groupCallMemberInfoList;
    private GroupCallFragment.OnCallEvents callEvents = null;

    public GroupCallMemberListAdapter(Context ctx, List<GroupCallMemberInfo> groupCallMemberInfoList, GroupCallFragment.OnCallEvents callEvents) {
        this.ctx = ctx;
        this.groupCallMemberInfoList = groupCallMemberInfoList;
        this.callEvents = callEvents;
        Log.debug(this.getClass()," MessageStatusAdapter groupCallMemberInfoList = " + groupCallMemberInfoList);
    }


    @Override
    public int getCount() {
        if(groupCallMemberInfoList != null){
            return groupCallMemberInfoList.size();
        }else{
            return 0;
        }
    }

    public void refresh(List<GroupCallMemberInfo> groupCallMemberInfoList){
        Log.debug(this.getClass(),"refresh groupCallMemberInfoList =  "+ groupCallMemberInfoList);
        this.groupCallMemberInfoList = groupCallMemberInfoList;
    }

    @Override
    public Object getItem(int position) {
        return groupCallMemberInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        GroupCallMemberInfo info = groupCallMemberInfoList.get(position);
        Log.debug(this.getClass(),"getView info =  "+ info);

        if (convertView == null)
        {
            LayoutInflater inflater = LayoutInflater.from(ctx);
            convertView = inflater.inflate(R.layout.group_call_member_item, null);

            viewHolder = new ViewHolder();
            viewHolder.nameTv = (TextView) convertView.findViewById(R.id.nameTv);
            viewHolder.videoViewIv = (ImageView) convertView.findViewById(R.id.videoViewIv);
            viewHolder.videoUploadIv = (ImageView) convertView.findViewById(R.id.videoUploadIv);

            convertView.setTag(viewHolder);
        }
        else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if(info.isRecv())
        {
            viewHolder.videoViewIv.setVisibility(View.VISIBLE);
        }
        else
        {
            viewHolder.videoViewIv.setVisibility(View.INVISIBLE);
        }
        if(info.isTrans())
        {
            Log.debug(this.getClass(),"getView isView =  "+ isView);
            if(isView)
            {
                viewHolder.videoUploadIv.setEnabled(false);
//                viewHolder.videoUploadIv.setVisibility(View.INVISIBLE);
            }
            else
            {
                viewHolder.videoUploadIv.setEnabled(true);
                viewHolder.videoUploadIv.setVisibility(View.VISIBLE);
            }
        }
        else
        {
            viewHolder.videoUploadIv.setVisibility(View.INVISIBLE);
        }
        viewHolder.videoUploadIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String jsonStr = info.getJsonStr();
                if(callEvents != null)
                {
                    isView = true;
                    Log.debug(this.getClass(),"isView = true");
                    callEvents.doUserAction(CallConst.BtnAction_Recv_Begin, jsonStr);
                    selectedPos = position;
                }
            }
        });
        String descStr = info.getGroupMembInfo().getDscp();
        viewHolder.nameTv.setText(descStr);
        return convertView;
    }

    public void setSelectedPos(int position) {
        selectedPos = position;
    }
    public int getSelectedPos() {
        return selectedPos;
    }
    public void setIsView(boolean isView)
    {
        Log.debug(this.getClass(),"setIsView isView= " + isView);
       this.isView = isView;
    }

    private class ViewHolder {
        public TextView nameTv;
        public ImageView videoViewIv;
        public ImageView videoUploadIv;
    }
}
